package com.nit.lab.vote.mapper.basic;

import com.nit.lab.vote.dto.basic.UserDTO;
import com.nit.lab.vote.entity.basic.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    @Select("SELECT *, user_id AS `key` from user ")
    List<UserDTO> listAll() throws Exception;

    @Insert("INSERT INTO user " +
            "(user_name, role_id, phone, password, original) " +
            "VALUES(#{entity.userName}, #{entity.roleId}, #{entity.phone}, #{entity.password}, #{entity.original})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    Integer add(@Param("entity")UserEntity entity) throws Exception;

    /**
     * @Description: 根据手机号获取用户信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"SELECT " +
            "* " +
            "FROM user " +
            "WHERE phone = #{phone} LIMIT 1"
    })
    UserDTO getByPhone(@Param("phone") String phone) throws Exception;

    /**
     * @Description: 根据用户ID获取用户信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"SELECT " +
            "* " +
            "FROM user " +
            "WHERE user_id = #{userId} "
    })
    UserDTO getByUserId(@Param("userId") Long userId) throws Exception;



    /**
     * @Description: 获取评委总条数
     * @Date: 2020/12/15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM user " +
            "WHERE role_id = 2 " +
            "<if test='userName != null'> AND user_name LIKE CONCAT('%', #{userName}, '%') </if>" +
            "<if test='phone != null'> AND phone LIKE CONCAT('%', #{phone}, '%') </if>" +
            "</script>"})
    Long countAllJudge(
            @Param("userName") String userName,
            @Param("phone") String phone) throws Exception;

    /**
     * @Description: 获取评委数据
     * @Date: 2020/12/15
     */
    @Select({"<script>SELECT " +
            "*, user_id AS `key` " +
            "FROM user " +
            "WHERE role_id = 2 " +
            "<if test='userName != null'> AND user_name LIKE CONCAT('%', #{userName}, '%') </if>" +
            "<if test='phone != null'> AND phone LIKE CONCAT('%', #{phone}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "user_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<UserDTO> listAllJudge(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("userName") String userName,
            @Param("phone") String phone,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @Description: 更新评委
     * @Date: 2020/12/15
     */
    @Update("UPDATE user " +
            "SET user_name = #{entity.userName}, " +
            "phone = #{entity.phone} " +
            "WHERE user_id = #{entity.userId}")
    Integer updateJudge(@Param("entity") UserEntity entity) throws Exception;

    /**
     * @Description: 删除评委
     * @Date: 2020/12/15
     */
    @Delete("DELETE FROM user " +
            "WHERE user_id = #{userId}")
    Integer removeJudge(@Param("userId") String userId) throws Exception;

    /**
     * @Description: 查找手机号
     * @Date: 2020/12/15
     */
    @Select("SELECT *, user_id AS `key` FROM user " +
            "WHERE phone = #{phone} LIMIT 1")
    UserDTO phoneCheck(@Param("phone") String phone) throws Exception;

    /**
     * @Description: 更新密码
     * @Date: 2020/12/15
     */
    @Update("UPDATE user " +
            "SET password = #{entity.password}, " +
            "phone = #{entity.original} " +
            "WHERE user_id = #{entity.userId}")
    Integer updatePassword(@Param("entity") UserEntity entity) throws Exception;
}
